define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods_orders/index' + location.search,
                    add_url: 'goods/goods_orders/add',
                    edit_url: 'goods/goods_orders/edit',
                    del_url: 'goods/goods_orders/del',
                    multi_url: 'goods/goods_orders/multi',
                    import_url: 'goods/goods_orders/import',
                    table: 'goods_orders',
                }
            });

            var table = $("#table");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user_id']", form).addClass("selectpage").data("source", "user/user/index").data("primaryKey", "id").data("field", "mobile").data("orderBy", "id desc");
                $("input[name='school_id']", form).addClass("selectpage").data("source", "school/school/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id'),events: Controller.api.events.user_id,formatter: Controller.api.formatter.user_id},
                        {field: 'school_id', title: __('学校'),events: Controller.api.events.school_id,formatter: Controller.api.formatter.school_id},
                        {field: 'goods.name', title: __('Goods_id'), operate:false},
                        {field: 'spec.name', title: __('Spec_id'), operate:false},
                        {field: 'size', title: __('Size'), operate:false},
                        {field: 'address.name', title: __('收货人'), operate:false},
                        {field: 'address.mobile', title: __('收获手机号'), operate:false},
                        {field: 'address.province', title: __('收货省'), operate:false},
                        {field: 'address.city', title: __('收货市'), operate:false},
                        {field: 'address.region', title: __('收货区'), operate:false},
                        {field: 'address.address', title: __('收货地址'), operate:false},

                        {field: 'total_amount', title: __('Total_amount'),  operate:false},
                        {field: 'coupon.coupon.name', title: __('Coupon_id'), operate:false},
                        {field: 'num', title: __('Num'), operate:false},
                        // {field: 'pay_type', title: __('Pay_type'), searchList: {"1":__('Pay_type 1'),"2":__('Pay_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'pay_amount', title: __('Pay_amount'), operate:false},
                        {field: 'paytime', title: __('Paytime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'mark', title: __('Mark'), operate:false, table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4'),"5":__('Status 5')}, formatter: Table.api.formatter.status},
                        {field: 'delivertime', title: __('Delivertime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'deliver_express.name', title: __('Deliver_express_id'), operate:false},
                        {field: 'deliver_expresswaybill', title: __('Deliver_expresswaybill'), operate:false},
                        {field: 'freight', title: __('Freight'), operate:false},
                        {field: 'coupon_amount', title: __('Coupon_amount'), operate:false},
                        {field: 'goods_amount', title: __('Goods_amount'), operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'goods/goods_orders/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'goods/goods_orders/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'goods/goods_orders/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                user_id: function (value, row, index) {

                    return '<a href="javascript:;" class="searchit btn-user_id" data-toggle="tooltip" data-original-title="点击搜索 ' + row.user.mobile + '"> ' + row.user.mobile + '</a>';
                },
                school_id: function (value, row, index) {

                    return '<a href="javascript:;" class="searchit btn-school_id" data-toggle="tooltip" data-original-title="点击搜索 ' + row.school.name + '"> ' + row.school.name + '</a>';
                },
            },
            events: {//绑定事件的方法
                user_id: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-user_id': function (e, value, row, index) {
                        e.stopPropagation();
                        var container = $("#table").data("bootstrap.table").$container;
                        //这里我们手动将数据填充到表单然后提交
                        $("form.form-commonsearch [name='user_id']", container).val(value);
                        $("form.form-commonsearch", container).trigger('submit');
                    }
                },
                school_id: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-school_id': function (e, value, row, index) {
                        e.stopPropagation();
                        var container = $("#table").data("bootstrap.table").$container;
                        //这里我们手动将数据填充到表单然后提交
                        $("form.form-commonsearch [name='school_id']", container).val(value);
                        $("form.form-commonsearch", container).trigger('submit');
                    }
                },
            }

        }
    };
    return Controller;
});
